<?php

namespace App\Exceptions;

use App\Response;
use Exception;

class UserException extends Exception
{
    /**
     * 初始化异常
     * @param string $message 错误信息
     * @param int $code 错误码
     * @param Closure $callback 回调
     */
    public function __construct( $message = '', $code = 0, \Closure $callback = null )
    {
        // code int
        $code = is_numeric( $code )? $code: 0;

        parent::__construct( $message, $code );

        // 执行回调
        $callback? $callback(): '';
    }

    // render
    public function render()
    {
        return Response::json( 
            [
                'code' => 0,
                'message' => 'success',
                'result' => [
                    'status' => $this->getCode(),
                    'message' => $this->getMessage()
                ]
            ] 
        );
    }
}
